<?php

class Core_Frontend_Component_Concessions_Controller_Content extends Core_Frontend_Share_Component_Controller {
    
    public function createAction(){
        $category = App::getModel('promotion/collection_concessions_category')->load();
        $pois = App::getModel('foundation/collection_poi')
                ->joinAccount()
                ->filterBy('accoundid',$this->getIdentity()->getId())
                ->load();
        $themes = App::getModel('design/collection_page_theme')
                        ->filterByType('concessions')
                        ->filterByEnabled(1)
                        ->load();
        return $this->viewResult(array('category'=>$category,'pois'=>$pois,'themes'=>$themes));
    }

    public function editAction($id){
        $model = App::getModel('promotion/concessions')->load($id);
        return $this->createAction($model->getAreaId())->setModel($model);
    }

    public function savePostAction() {
        if ($this->saveModel('draft', $model)) {
            return $this->redirectResult($this->getUrl(array('subject_id'=>$this->getSession()->get('current_subject_id')),'subjectconcessions'));
        }
        return $this->createAction()->setModel($model);
    }

    private function saveModel($state, &$savedModel = NULL) {
        $data = $this->getRequest()->getForm('concessions');
        $subject = App::getModel('subject/content')->load($this->getSession()->get('current_subject_id'));
        $model = App::getModel('promotion/concessions');
        if (isset($data['id']) && !empty($data['id'])) {
            $model->load($data['id']);
            $subject->unassociateConcessions($data['id']);
        }
        $model->addData($data)->setStatus($state)->setOwnerId($this->getIdentity()->getId());
        $savedModel = $model;
        if (($errors = $model->validate() === TRUE)) {
            $model->save();
            $subject->associateConcessions($model->getId());
            $this->getMessager()->addInform($this->__f('share.message.saveSuccess', '{promotion.label.concessions}', $model->getName()));
             return TRUE;
             } else {

            $this->getMessager()->addError($this->__f('share.message.saveFail', '{article.label.content}', $model->getTitle()))
                    ->addError($errors);
            return FALSE;
        }
    }

    public function detailAction($id) {
        $concessions = App::getModel('promotion/concessions')->updateClickCount($id)->load($id);
        $themeId = $concessions->getThemeId();
        if (empty($themeId)) {
            $themeId = $concessions->getCategory()->getThemeId();
        }
        $pageTemplate = App::getModel('design/collection_page_template')
                        ->filterByPageThemeId($themeId)
                        ->filterByFileName('detail')
                        ->filterByEnabled(1)
                        ->load()
                        ->getFirstItem();
        $this->getVariables()->setData('page_template', $pageTemplate);
        $this->getVariables()->setData('concessions_object', $concessions);
        $this->getPage()->getNavigation()->addLink('优惠列表', $this->getUrl(NULL,'conlist', 'content'))->addLabel($concessions->getName());
        $this->getPage()->setTitle($concessions->getName())
                ->setKeyword($concessions->getTag())
                ->addMeta('name', 'description', $concessions->getIntroduction());
        return $this->pageResult($pageTemplate);
    }

    public function conlistAction($id = NULL, $page = 1) {
        $themeId = 'c58b96b9e88e36df011058f782853ebe';
        
        if ($id) {
            $themeId = App::getModel('promotion/concessions')->load($id)->getThemeId();
        }

        $pageTemplate = App::getModel('design/collection_page_template')
                        ->filterByPageThemeId($themeId)
                        ->filterByFileName('conlist')
                        ->filterByEnabled(1)
                        ->load()
                        ->getFirstItem();
        if($page == 1){
            $startIndex = 0;
            $pageSize = 13;
        }
        else if($page>1){
            $startIndex = 13+($page-2)*20;
            $pageSize = 20;
        }
        $concessions_list = App::getModel('promotion/collection_concessions')
                        ->filterByStatus('published')
                        ->limit($pageSize,$startIndex);
        if (!is_null($id)) {
            $concessions_list->filterByCategoryId($id);
        }
        $concessions_list->load();
        $records = $concessions_list->loadCount();
        
        $args = array_merge($this->getRequest()->getAllParameters(), array('page' => '{index}'));
        $pagination = new Kd_Html_Pagination();
        $pagination->setPageIndex($page)
                ->setPageSize($pageSize)
                ->setPageRange(6)
                ->setRecordCount($records)
                ->setUrlFormat($this->getUrl($args));

        $this->getVariables()
                ->setData('concessions_list', $concessions_list)
                ->setData('concessions_list_count', $records)
                ->setData('concessions_list_page_index', $page)
                ->setData('concessions_list_page_size', 20)
                ->setData('concessions_list_pagination', $pagination)
                ->setData('page_template', $pageTemplate);
        Kd_Web::getPage()->getNavigation()->addLabel('优惠列表');
        Kd_Web::getPage()->setTitle('列表页')->setKeyword('');
        return $this->pageResult($pageTemplate);
    }

    public function subjectConcessionsAction($subject_id,$page=1,$sort = NULL){
        $subject = App::getModel('subject/content')->load($subject_id);
        $concessions = $subject->getConcessions()
                            ->filterBy('ownerid',$this->getIdentity()->getId())
                            ->filterBy('status','published');
        if(!is_null($sort)){
            $concessions->sortBy($sort,'desc');
        }
        $concessions->load();
        $records = $concessions->loadCount();
        $args = array_merge($this->getRequest()->getAllParameters(), array('page' => '{index}'));
        $pagination = new Kd_Html_Pagination();
        $pagination->setPageIndex($page)
                   ->setPageSize(10)
                   ->setPageRange(6)
                   ->setRecordCount($records)
                   ->setUrlFormat($this->getUrl($args));
        Kd_Web::getPage()->getNavigation()
                ->addLink($subject->getName(),$this->getUrl(NULL,'showSubject','profile','proxy'))
                ->addLabel('编辑优惠');
        Kd_Web::getPage()->setTitle('编辑优惠')->setKeyword('');
        $this->getSession()->set('current_subject_id',$subject_id);
        $this->getVariables()
                ->setData('concessions_list', $concessions)
                ->setData('concessions_list_count', $concessions->loadCount())
                ->setData('concessions_list_page_index', $page)
                ->setData('concessions_list_page_size', 20)
                ->setData('concessions_list_pagination', $pagination);
        return $this->viewResult(array('concessions'=>$concessions,'pagination'=>$pagination));
    }

    public function previewAction($id){
        if($this->getSession()->has('is_concessions_preview')){
            $this->getSession()->remove('is_concessions_preview');
            return $this->redirectResult($this->getUrl(array('id'=>$id),'detail','content','concessions'));
        }
        die('非法操作');
    }

}